package com.alliky.home.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alliky.core.base.BaseFragment;
import com.alliky.core.entity.vo.BannerEntityInfo;
import com.alliky.core.global.Global;
import com.alliky.core.router.RoutePath;
import com.alliky.core.util.AesUtil;
import com.alliky.core.util.ImageLoader;
import com.alliky.core.util.KLog;
import com.alliky.home.BR;
import com.alliky.home.R;
import com.alliky.home.ui.adapter.CategoryAdapter;
import com.alliky.home.ui.adapter.HomeHotGoodsAdapter;
import com.alliky.home.databinding.FragmentHomeBinding;
import com.alliky.home.ui.viewmodel.HomeViewModel;

import java.util.ArrayList;

@Route(path = RoutePath.Home.FRAGMENT_HOME)
public class HomeFragment extends BaseFragment<FragmentHomeBinding, HomeViewModel> {

    CategoryAdapter mCategoryAdapter;
    HomeHotGoodsAdapter mHomeHotGoodsAdapter;
    boolean isRefresh = true;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_home;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        //通过binding拿到toolbar控件, 设置给Activity
        ((AppCompatActivity) getActivity()).setSupportActionBar(binding.include.toolbar);
        //初始化标题
//        mViewModel.initToolbar();

        binding.xbanner.loadImage((banner, model, view, position) ->
                ImageLoader.loadImage(getContext(), ((BannerEntityInfo) model).getBannerEntity().getImage(), (ImageView) view)
        );

        binding.xbanner.setOnItemClickListener((banner, model, view, position) -> ARouter.getInstance().build(RoutePath.Web.ACTIVITY_WEBVIEW)
                .withString(Global.KEY.TITLE, ((BannerEntityInfo) model).getBannerEntity().getTitle())
                .withString(Global.KEY.URL, ((BannerEntityInfo) model).getBannerEntity().getLink())
                .navigation());

        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        binding.recyclerViewHot.setLayoutManager(manager);
        mViewModel.getBannerData();
        mViewModel.getCategoryData();

        binding.refreshLayout.autoRefresh();

        binding.recyclerViewCate.setLayoutManager(new GridLayoutManager(getContext(), 5));

        mCategoryAdapter = new CategoryAdapter(new ArrayList<>());
        binding.recyclerViewCate.setAdapter(mCategoryAdapter);

        binding.recyclerViewHot.setAdapter(mHomeHotGoodsAdapter = new HomeHotGoodsAdapter(new ArrayList<>()));

        binding.refreshLayout.setOnRefreshListener(refreshLayout -> {
            isRefresh = true;
            mViewModel.getHotGoods(true);
        });

        binding.refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            isRefresh = false;
            mViewModel.getHotGoods(false);
        });

        KLog.d(">>>>>>>", AesUtil.encrypt("123456", AesUtil.AESKey));
    }

    @Override
    public void initViewObservable() {
        mViewModel.bannerEntity.observe(this, o -> binding.xbanner.setBannerData(o));

        mViewModel.finishRefresh.observe(this, o -> binding.refreshLayout.finishRefresh());

        mViewModel.finishLoadMore.observe(this, o -> binding.refreshLayout.finishLoadMore());

        mViewModel.getCategoryDataEvent.observe(this, categoryVOS -> {
            if (mCategoryAdapter != null) {
                mCategoryAdapter.setNewInstance(categoryVOS);
            }
        });

        mViewModel.getHotGoodsEvent.observe(this, goodsVOS -> {
            if (mHomeHotGoodsAdapter != null) {
                if (isRefresh) {
                    mHomeHotGoodsAdapter.setNewInstance(goodsVOS);
                } else {
                    mHomeHotGoodsAdapter.addData(goodsVOS);
                }
            }
        });
    }
}
